package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.able.UserAbleItem;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserAbleItemDaoImpl extends TempDataObjectDao<UserAbleItem> {

    public static String getUserAbleItemSql = "select * from `t_u_able_item`";

    public Map<Long, Map<Integer, UserAbleItem>> getUserAbleItemMap() {
        Map<Long, Map<Integer, UserAbleItem>> mapMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserAbleItemSql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserAbleItem item = parse(rs);
                if (!mapMap.containsKey(item.getUserId())) {
                    mapMap.put(item.getUserId(), new ConcurrentHashMap<>());
                }
                mapMap.get(item.getUserId()).put(item.getGoodsId(), item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return mapMap;
    }

    public static String updateUserAbleItemSql = "update `t_u_able_item` set num=?,modifyTime=? where userId=? and goodsId=?";

    @Override
    protected boolean updateData(UserAbleItem userAbleItem) {
        userAbleItem.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userAbleItem.getNum()));
        para.put(i++, new DbParameter(Types.BIGINT, userAbleItem.getModifyTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userAbleItem.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userAbleItem.getGoodsId()));
        if (getDb().executeNoneQuery(updateUserAbleItemSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserAbleItemSql = "insert into `t_u_able_item`(userId,goodsId,num,modifyTime) values (?,?,?,?)";

    @Override
    protected boolean addData(UserAbleItem userAbleItem) {
        userAbleItem.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userAbleItem.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userAbleItem.getGoodsId()));
        para.put(i++, new DbParameter(Types.BIGINT, userAbleItem.getNum()));
        para.put(i++, new DbParameter(Types.BIGINT, userAbleItem.getModifyTime()));
        if (getDb().executeNoneQuery(insertUserAbleItemSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserAbleItem parseData(ResultSet rs) throws SQLException {
        UserAbleItem userAbleItem = new UserAbleItem();
        userAbleItem.setUserId(rs.getLong("userId"));
        userAbleItem.setGoodsId(rs.getInt("goodsId"));
        userAbleItem.setNum(rs.getLong("num"));
        userAbleItem.setModifyTime(rs.getLong("modifyTime"));
        return userAbleItem;
    }

}
